﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using VAdvantage.Model;
using System.Data;
using VAdvantage.Utility;
using System.Threading;
using System.Windows.Threading;
using VAdvantage.Logging;

namespace VAdvantage.MailBox.Classes
{
    public class MailConfigMethod
    {
        static bool chk = false;
       static SMTPConfig config = new SMTPConfig();
       private static VLogger _log = VLogger.GetVLogger(typeof(MailConfigMethod).FullName);
        public static SMTPConfig GetUSmtpConfig(int AD_User_ID)
        {
            //IMAPConfig localConfig = new IMAPConfig();
            
           
            MUserMailConfigration _userMailConfigration = null;

            IDataReader idr=null;

            string sql = "SELECT * FROM AD_USERMAILConfigration WHERE AD_USER_ID=" + Envs.GetContext().GetAD_User_ID() + " AND IsActive='Y'";


            sql = MRole.GetDefault().AddAccessSQL(sql, "AD_USERMAILConfigration", MRole.SQL_FULLYQUALIFIED, MRole.SQL_RO);
            
               idr  = DataBase.DB.ExecuteReader(sql, null, null);

               //DataTable dt = new DataTable();
               //dt.Load(idr);


            if(idr.Read())
            {
                   _userMailConfigration = new MUserMailConfigration(Envs.GetCtx(),idr, null);
               }
            if (idr != null)
                idr.Close();
               //config = new IMAPConfig(System.Windows.Forms.Application.StartupPath + "\\" + DataBase.GlobalVariable.IMAP_CONFIG_FILE);
               if (_userMailConfigration != null)
               {
                   if (string.IsNullOrEmpty(_userMailConfigration.GetSmtpHost()) || string.IsNullOrEmpty(_userMailConfigration.GetSmtpUsername()) || string.IsNullOrEmpty(_userMailConfigration.GetSmtpPassword()))
                   {
                       _log.Info("Config info not foundM1");
                       return null;
                   }

                   config = new SMTPConfig(_userMailConfigration.GetSmtpHost(),
                                       _userMailConfigration.GetSmtpUsername(), _userMailConfigration.GetSmtpPassword(),
                                       _userMailConfigration.IsSmtpIsSsl(), _userMailConfigration.IsSmtpAuthorization(), false, "",_userMailConfigration.GetEMail());
               }
               else
               {
                   // Classes.ShowMessage.Error("ConfigNotFound", true);
                   _log.Info("Config info not foundM2");
                   return null;
               }
            chk = true;
            _log.Info("Config info found");
               return config;
            
            return config;
        }

        public static SMTPConfig GetUMConfig(int AD_User_ID)
        {
            //IMAPConfig localConfig = new IMAPConfig(); //used for any saved on local pc 
            //IMAPConfig config = new IMAPConfig(); // user for user configuration
            MUserMailConfigration _userMailConfigration = null;

            try
            {

                string sql = "Select * from AD_USERMAILConfigration where AD_USER_ID=" + Envs.GetContext().GetAD_User_ID() + "and IsActive='Y'";
                IDataReader idr = DataBase.DB.ExecuteReader(sql, null, null);
                if (idr.Read())
                {
                    _userMailConfigration = new MUserMailConfigration(Envs.GetCtx(), idr, null);

                }
                if (idr != null)
                    idr.Close();
                //config = new IMAPConfig(System.Windows.Forms.Application.StartupPath + "\\" + DataBase.GlobalVariable.IMAP_CONFIG_FILE);
                if (_userMailConfigration != null)
                {
                    //config = new IMAPConfig(_userMailConfigration.GetImapHost(),
                    //                    _userMailConfigration.GetImapUsername(), _userMailConfigration.GetImapPassword(), _userMailConfigration.GetEMail(),
                    //                    _userMailConfigration.IsImapIsSsl(), _userMailConfigration.IsAutoLogin(), _userMailConfigration.IsAutoAttach(), _userMailConfigration.GetTableAttach(), "");
                    config.Host=_userMailConfigration.GetImapHost();
                    config.UserName=_userMailConfigration.GetImapUsername();
                    config.Password=_userMailConfigration.GetImapPassword();
                    config.UseSSL=_userMailConfigration.IsImapIsSsl();
                    config.AutoLogon=_userMailConfigration.IsAutoLogin();
                    config.Email = _userMailConfigration.GetEMail();
                    //config.URL = _userMailConfigration.GetURL();

                }
                else
                {
                    // Classes.ShowMessage.Error("ConfigNotFound", true);
                    return null;
                }
                return config;
            }
            catch
            {
                return null;
            }

            // Get the local saved Config file if any
            //localConfig = new IMAPConfig(configFile);


        }

        public static String GetURL()
        {
 
            string sql = "Select URL from AD_Client where AD_Client_ID=" + Envs.GetContext().GetAD_Client_ID() + "and IsActive='Y'";

            try
            {
                IDataReader idr = DataBase.DB.ExecuteReader(sql, null, null);
                string url = "";
                if (idr.Read())
                {
                    url = idr[0].ToString();
                }
                if (idr != null)
                    idr.Close();

                return url;
            }
            catch
            {
                return "";
            }

        }
      
    }
}
